<?php
namespace app\admin\controller;

use think\Db;

class Filedownload extends Base{
	/**
	 * [列表]
	 */
	public function index() {
		if($this->request->isPost()){
			$param = input("post.");
			$limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			$return['rows'] = DB::name("attachment_file")
				->limit($offset,$limit)
				->order("is_top asc")
				->order("publish_time desc")
				->order("id desc")->select();
			foreach($return['rows'] as $k => $v){
				if($return['rows'][$k]['publish_time'] != 0){
					$return['rows'][$k]['publish_time_name'] = date("Y-m-d H:i",$v['publish_time']);
				}
				if($v['is_top'] == 0){
					$return['rows'][$k]['is_top_name'] = "已置顶";
				}else{
					$return['rows'][$k]['is_top_name'] = "未置顶";
				}
				if($v['is_publish'] == 1){
					$return['rows'][$k]['is_publish_name'] = "已发布";
				}else{
					$return['rows'][$k]['is_publish_name'] = "未发布";
				}
			}
			$return['total'] =  DB::name("attachment_file")->count();
			return ['code'=>1,'msg'=>'表格加载成功','data'=>$return];
		}
		return $this->fetch();
	}
	/**
	 * [首页列表]
	 */
	public function getIndexTable() {
		$param = input("post.");
		$limit = $param['pageSize'];
		$offset = ($param['pageNumber'] - 1) * $limit;
		$return['rows'] = DB::name("attachment_file")
			->where('is_publish','1')
			->limit($offset,$limit)
			->order("is_top asc")
			->order("publish_time desc")
			->order("id desc")->select();
		foreach($return['rows'] as $k => $v){
			if($return['rows'][$k]['publish_time'] != 0){
				$return['rows'][$k]['publish_time_name'] = date("Y-m-d H:i",$v['publish_time']);
			}
			if($v['is_top'] == 0){
				$return['rows'][$k]['is_top_name'] = "已置顶";
			}else{
				$return['rows'][$k]['is_top_name'] = "未置顶";
			}
			if($v['is_publish'] == 1){
				$return['rows'][$k]['is_publish_name'] = "已发布";
			}else{
				$return['rows'][$k]['is_publish_name'] = "未发布";
			}
		}
		$return['total'] =  DB::name("attachment_file")->count();
		return ['code'=>1,'msg'=>'表格加载成功','data'=>$return];
	}
	/**
	 * [删除]
	 * 1.删除snake_attachment_file
 	 * 2.删除snake_attachment
 	 * 3.删除文件
	 */
	public function delFile() {
		$param = input('post.param/a');
		DB::startTrans();
		try{
			foreach($param as $k => $v){
				$info = DB::name('attachment_file')->where('id',$v)->find();
				DB::name('attachment_file')->where('id',$v)->delete();
				DB::name('attachment')->where('id',$info['attachment_id'])->delete();
				if(file_exists(APP_PATH."../public/".$info['url']) && $info['url'] != ''){
					unlink(APP_PATH."../public/".$info['url']);
				}
			}
			DB::commit();
			return ['code'=>1,'msg'=>'删除成功'];
		}catch(\Exception $e){
			DB::rollback();
			return ['code'=>0,'msg'=>'删除失败:'.$e->getMessage()];
		}
	}
	/**
	 * [置顶 / 取消]
	 */
	public function topFile() {
		$param = input('post.param/a');
		DB::startTrans();
		try{
			foreach($param as $k => $v){
				$top = DB::name('attachment_file')->where('id',$v)->value('is_top');
				$top == 0 ? $data = ['is_top' => 9] : $data = ['is_top' => 0];
				DB::name('attachment_file')->where('id',$v)->update($data);
			}
			DB::commit();
			return ['code'=>1,'msg'=>'置顶 / 取消置顶 成功'];
		}catch(\Exception $e){
			DB::rollback();
			return ['code'=>0,'msg'=>'置顶 / 取消置顶 失败:'.$e->getMessage()];
		}
	}
	/**
	 * [发布 / 取消]
	 */
	public function pubFile() {
		$param = input('post.param/a');
		DB::startTrans();
		try{
			foreach($param as $k => $v){
				$publish = DB::name('attachment_file')->where('id',$v)->value('is_publish');
				$publish == 0 ?
					$data = ['is_publish' => 1,'publish_time'=>time()] :
					$data = ['is_publish' => 0,'publish_time'=>0];
				DB::name('attachment_file')->where('id',$v)->update($data);
			}
			DB::commit();
			return ['code'=>1,'msg'=>'发布 / 取消发布 成功'];
		}catch(\Exception $e){
			DB::rollback();
			return ['code'=>0,'msg'=>'发布 / 取消发布 失败:'.$e->getMessage()];
		}
	}

	/**
	 * [添加文件]
	 */
	public function addFile() {
		if($this->request->isPost()){
			$param = input("post.");
			if($param['is_publish'] == '1'){
				$param['publish_time'] = time();
			}
			$param['create_uid'] = session('id');
			$res = DB::name('attachment_file')->insert($param);
			if($res !== false){
				return ['code'=> 1,'msg'=>'添加文件成功','data'=>'index'];
			}else{
				return ['code'=> 0,'msg'=>'添加文件失败','data'=>''];
			}
		}
		return $this->fetch();
	}
	/**
	 * [header强制下载]
	 */
	public function myDownloadFile(){
		$url = input("url");
		$path = APP_PATH."../public/".$url;
		if(file_exists(iconv('UTF-8','GB2312',$path)) && $url != ''){
			header('Pragma: public'); // required
			header('Expires: 0'); // no cache
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Cache-Control: private',false);
			header('Content-Type: application/force-download');
			header('Content-Disposition: attachment; filename="'.basename($path).'"');
			header('Content-Transfer-Encoding: binary');
			header('Connection: close');
			@readfile($path); // push it out
			exit();
		}else{
			$this->error("该文件已丢失");
		}
	}
}
